<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Backend_Auth extends Controller_Backend {
    
    public function action_index()
    {
        $this->action_login();
    }

    public function action_login()
    {

       if($this->auth->logged_in('admin'))
            $this->request->redirect('backend');

        if($this->request->method() == Request::POST)
        {
            $data = Arr::extract($this->request->post(), array('username', 'password', 'remember'));

            $status = $this->auth->login($data['username'], $data['password'], (bool) $data['remember']);
            if($status)
            {
                if($this->auth->logged_in('admin'))
                    $this->request->redirect('backend');
            }
            else
            {
                $errors = array(__(Kohana::message('validation', 'no_user')));
            }

        }
        
        $content = View::factory('backend/auth/v_auth_login')
            ->bind('errors', $errors)
            ->bind('data', $data);

        $this->template->title_page = __('Log in');
        $this->template->content = $content;
    }

    public function action_logout()
    {
        if($this->auth->logout())
            $this->request->redirect();
    }

}
